const mathMound = (value, n) => {
    n = n === undefined ? 1 : n;
    let bei = Math.pow(10, n);
    let res = Math.round(value * bei) / bei;
    return +parseFloat(res.toPrecision(12));
};

// 如果是etsy，要对富文本做处理，不支持换行
const getEtsyDescription = (state) => {
    // 如果是etsy，要对富文本做处理，不支持换行
    if (state.shop.shop_type == 2) {
        let html = state.setting.description;
        // 块元素变换行
        html = html.replace(/<(li|div|p)[^\>]*?>/g, "<br>");
        // 换行元素转\n
        html = html.replace(/(<br\/?>)+/g, "\n");
        // 其他元素去掉
        html = html.replace(/<\/?.+?>/g, "");
        // 去重复换行
        html = html.replace(/(\r?\n)+/g, "\n");
        // 去头尾换行
        html = html.replace(/(^\n|\n$)/g, "");
        // 换行转br
        html = html.replace(/\r?\n/g, "<br>");

        return html;
    } else {
        return state.setting.description;
    }
};

// 格式化颜色和尺码信息
const getInventoryFormat = (inventory_list) => {
    const color_map = new Map();
    const sizes_map = new Map();
    const result = {
        all_list: inventory_list || [],
        color_list: [],
        sizes_list: [],
        select_color: [],
        select_sizes: [],
    };

    // 排序！！！重要
    result.all_list.sort((a, b) => {
        const asort = a.size_sort * 100 + a.color_sort;
        const bsort = b.size_sort * 100 + b.color_sort;
        return asort - bsort;
    });

    // 选颜色和默认颜色
    result.all_list.map((item, index) => {
        // 只有大于-999的才可以计算
        if (item.quality <= -999) {
            // 库存不足
        } else {
            // 选中的颜色
            if (!color_map.get(item.color_id)) {
                color_map.set(item.color_id, true);
                result.color_list.push(item);
            }

            if (!sizes_map.get(item.size_id)) {
                sizes_map.set(item.size_id, true);
                result.sizes_list.push(item);
            }
        }
    });

    // 排序
    result.color_list.sort((a, b) => a.color_sort - b.color_sort);
    result.sizes_list.sort((a, b) => a.size_sort - b.size_sort);

    // 颜色默认选中
    result.select_color = result.color_list.map((i) => i.color_id);
    // 尺码默认选中
    result.select_sizes = result.sizes_list.map((i) => i.size_id);

    return result;
};

// 格式化价格
const getVariantByStore = (setting, product, inventory_list) => {
    const cache = new Map();

    const level_discount = setting.user_discount || 100;
    const tag_design_info = setting.tag_design_info || {};
    const pack_design_info = setting.pack_design_info || {};
    const tag_price = parseFloat(tag_design_info.total_price || 0);
    const pack_price = parseFloat(pack_design_info.total_price || 0);

    const extra_price = parseFloat(product.extra_price) || 0;
    const blank_discount = product.is_blank ? 0.95 : 1;
    const rate = setting.rate;

    inventory_list.map((item) => {
        cache.set(item.size_id + "-" + item.color_id, item);
    });

    return (product.variants || []).map((item) => {
        const select = cache.get(item.size + "-" + item.color) || {};

        // 商品原价
        const original = select.price + extra_price;

        // 成本价要重新计算，因为包装吊牌、用户等级折扣都是动态的
        const cost_blank = Math.ceil(original * blank_discount);
        const cost_product = Math.round(0.01 * cost_blank * level_discount);
        const cost_tag = Math.round(0.01 * tag_price * level_discount);
        const cost_pack = Math.round(0.01 * pack_price * level_discount);

        const cost = Math.ceil(rate * (cost_product + cost_tag + cost_pack));

        const profit = item.retail_price - cost;
        const point = item.retail_price
            ? mathMound((100 * profit) / item.retail_price, 0).toFixed(2)
            : "";

        return {
            sku: select.sku,
            original: original,
            cost: cost, // 成本价
            retail: item.retail_price, // 零售价
            profit: profit, // 利润
            point: point, // 利润率

            color_id: item.color,
            color_name: select.color_name,
            color_num: select.color_num,

            size_id: item.size,
            size_name: select.size_name,
            quality: select.quality,
        };
    });
};

const state = () => ({
    // 状态
    status: {
        id: "", // 商店商品 id
        step: 1, // 当前进度
        action: "", // 操作
    },
    shop: {
        shop_id: 0, // 商店id
        shop_name: "",
        shop_type: "",
        ss_currency: "",
        img_limit: 10,
        platform: "", // 平台
        pack_design_info: null, // 商店默认包装袋
        tag_design_info: null, // 商店默认吊牌
    },

    // 共用的设置
    setting: {
        title: "", // 标题
        description: "", // 商品描述
        user_level: 1, // 用户等级折扣
        user_discount: 100, // 用户等级折扣
        tags: "", // 标签
        display: true, // 是否显示在商店中
        taxonomy_id: "",
        is_new_shipping: true,
        shipping_profile_id: "",
        tag_design_info: null, // 吊牌
        pack_design_info: null, // 包装
        product_collection_id: "", // shopify 分类id ，number类型
        product_collection: "", // 产品所属分类, wix 分类id string类型

        // 一些个人设置
        currency: "USD", // 货币
        rate: 1, // 汇率
        price_notify: "change", // 是否
    },
    // 衣服列表
    clothes: [],

    // 同步的图片
    mockups: [],
    // 缓存
    mockups_cache: [],
});

const getters = {
    getRequest(state) {
        // 请求体数据结构
        const request = {
            id: state.status.id || "", // 商店商品 id
            shop_id: state.shop.shop_id || "", // 商店 id
            title: state.setting.title || "", // 标题
            vendor: "podpartner", // 供应商，目前固定为 PODpartner
            product_collection_id: state.setting.product_collection_id || "", // shopify 商品分类 |  etsy不用  | Squarespace不用
            product_collection: state.setting.product_collection || "", // wix 商品分类 | squarespace 的 StorePageID 共用
            display: state.setting.display || false, // 是否在商店显示 true显示 false 不显示
            shipping_profile_id: state.setting.shipping_profile_id || "", // etsy 的 ShippingProfile ID
            is_new_shipping: state.setting.is_new_shipping || false, // true 的话，新建模板 false 使用 shipping_profile_id
            taxonomy_id: state.setting.taxonomy_id || "", // etsy 商品分类模板
            currency: state.setting.currency, // 货币

            tags: "", // 多个 tag 连接成一个字符串，以|隔开
            body_html: getEtsyDescription(state), // 描述
            tag_design_id: "", // 与模板绑定的 吊牌设计ID
            pack_design_id: "", // 包装设计ID
            image_infos: [], // 要同步的图片 url，序列化以后的
            design_data: [], // 差异化的设计信息，序列化以后的
        };

        // wix 不需要传tags
        if (state.shop.shop_type == 3) {
            request.tags = "";
        } else {
            request.tags = state.setting.tags || "";
        }

        // 吊牌
        if (state.setting.tag_design_info) {
            request.tag_design_id = state.setting.tag_design_info.id || "";
        }

        // 包装袋
        if (state.setting.pack_design_info) {
            request.pack_design_id = state.setting.pack_design_info.id || "";
        }

        // 图片数据,要在synchronize.vue 中重新处理
        request.image_infos = state.mockups;

        // 设计信息
        request.design_data = state.clothes.map((product, index, clothes) => {
            const select_sizes = product.select_sizes || [];
            const select_color = product.select_color || [];

            // 变体价格
            const variants = (product.variants || []).map((item) => {
                const size_abled = select_sizes.indexOf(item.size_id) >= 0;
                const color_abled = select_color.indexOf(item.color_id) >= 0;
                const stock_abled = item.quality > -999;

                return {
                    color: item.color_id,
                    size: item.size_id,
                    cost: item.cost, // 原价
                    retail_price: item.retail, // 零售价
                    sku: item.sku,
                    disabled: !size_abled || !color_abled || !stock_abled,
                };
            });

            // 客户自定义颜色名称的序列化
            const color_names = (product.color_names || []).map((item) => {
                return {
                    color_id: item.color_id,
                    color_num: item.color_num,
                    custom_name: item.name,
                };
            });

            return {
                role_type: clothes.length == 1 ? 1 : index == 0 ? 10 : 20, // 0：旧数据，1：普通的设计，10：套装主设计，20：套装从设计
                design_id: product.design_id, // 模板 id
                product_id: product.product_id, // 商品 id
                shop_product_id: product.shop_product_id || 0, // 商品 id
                sub_title: product.sub_title,
                size: select_sizes, // 所有尺码 id
                color: select_color, // 所有颜色 id
                variants: variants, // 变体及价格
                custom_color_infos: clothes.length == 1 ? [] : color_names, // 客户自定义颜色名称的序列化,只有一个时传空
            };
        });

        return request;
    },
};

const mutations = {
    // 删除产品
    DEL_PRODUCT(state, payload) {
        const index = state.clothes.indexOf(payload);
        if (index >= 0) {
            state.clothes.splice(index, 1);
            state.mockups = [];
            this.commit("synchronize/SET_BRAND");
            this.commit("synchronize/SET_SETTING");
            this.commit("synchronize/SET_VARIANTS", state.clothes);
        }
    },

    // 设置选中颜色
    SET_SELECT_COLOR(state, payload) {
        if (!payload || !payload.product || !payload.select_color) {
            // 参数不对
        } else {
            const product = payload.product;
            const select = payload.select_color;
            const ids = product.color_list.map((i) => i.color_id);

            // 去重处理
            const norepeat = select.filter(
                (c, i, arr) => i === arr.indexOf(c, 0)
            );

            // 排序
            norepeat.sort((a, b) => ids.indexOf(a) - ids.indexOf(b));

            product.select_color = norepeat;
            this.commit("synchronize/SET_COLOR_NAMES", product);
        }
    },

    // 设置选中尺寸
    SET_SELECT_SIZES(state, payload) {
        if (!payload || !payload.product || !payload.select_sizes) {
            // 参数不对
        } else {
            const product = payload.product;
            const select = payload.select_sizes;
            const ids = product.sizes_list.map((i) => i.size_id);

            // 去重处理
            const norepeat = select.filter(
                (c, i, arr) => i === arr.indexOf(c, 0)
            );

            // 排序
            norepeat.sort((a, b) => ids.indexOf(a) - ids.indexOf(b));
            payload.product.select_sizes = norepeat;
        }
    },

    // 设置颜色名称
    SET_COLOR_NAMES(state, product) {
        if (!product || state.clothes.indexOf(product) < 0) {
            // 参数不对
        } else {
            const color_names = product.color_names || [];
            const color_list = product.color_list || [];

            const name_cache = new Map();
            const color_cache = new Map();
            const stock_cache = new Map();

            const result = [];
            const ids = product.color_list.map((i) => i.color_id);

            color_names.map((i) => name_cache.set(i.color_id, i));
            color_list.map((i) => color_cache.set(i.color_id, i));

            // 要考虑库存不足的情况
            product.inventory_list.map((item) => {
                if (product.select_color.indexOf(item.color_id) < 0) {
                    // 颜色没选中不管
                } else if (product.select_sizes.indexOf(item.size_id) < 0) {
                    // 尺码没选中不管
                } else if (stock_cache.get(item.color_id)) {
                    stock_cache.get(item.color_id).push(item.quality);
                } else {
                    stock_cache.set(item.color_id, [item.quality]);
                }
            });

            // 排序
            product.select_color.map((color_id) => {
                // 库存
                const stock = stock_cache.get(color_id) || [];
                const outof = stock.filter((i) => i <= -999);

                if (outof.length == stock.length) {
                    // 库存不足
                } else if (name_cache.get(color_id)) {
                    result.push(name_cache.get(color_id));
                } else {
                    const color = color_cache.get(color_id);
                    result.push({
                        color_id: color_id,
                        color_name: color.color_name,
                        color_num: color.color_num,
                        name: product.title + "-" + color.color_name,
                    });
                }
            });

            result.sort((a, b) => ids.indexOf(a) - ids.indexOf(b));

            product.color_names = result;
        }
    },

    // 修改品牌
    SET_BRAND(state, payload) {
        const shop = state.shop;

        const tag = state.clothes.filter((p) => p.tag_design_info)[0];
        const pack = state.clothes.filter((p) => p.pack_design_info)[0];

        if (tag) {
            state.setting.tag_design_info = tag.tag_design_info;
        } else if (shop.tag_design_info) {
            state.setting.tag_design_info = shop.tag_design_info;
        } else {
            state.setting.tag_design_info = null;
        }

        if (pack) {
            state.setting.pack_design_info = pack.pack_design_info;
        } else if (shop.pack_design_info) {
            state.setting.pack_design_info = shop.pack_design_info;
        } else {
            state.setting.pack_design_info = null;
        }
    },

    // 设置模特预览图
    SET_MOCKUPS(state, payload) {
        if (payload && Array.isArray(payload)) {
            state.mockups = payload;
        } else {
            const mockups = [];
            const limit = Math.min(state.shop.img_limit, 10);

            // 初始化
            // 默认10张图，一张主图
            state.clothes.map((product) => {
                product.select_color.map((color_id) => {
                    // 超过10张不添加
                    if (mockups.length + state.clothes.length < limit) {
                        // 选择默认的种类
                        const model_tag = product.model_tag;
                        const model =
                            product.model_list.find(
                                (m) =>
                                    m.model_tag == model_tag &&
                                    m.surface_name == "Front Side"
                            ) || product.model_list[0];
                        mockups.push({
                            type: mockups.length == 0 ? "main" : "normal", // 类型： 主图main|普通normal|尺码size
                            design_id: product.design_id,
                            color_id: color_id,
                            model_id: model.id,
                        });
                    }
                });
            });

            state.clothes.map((product) => {
                // 添加尺码
                mockups.push({
                    type: "size",
                    design_id: product.design_id,
                    color_id: "",
                    model_id: "",
                });
            });
            state.mockups = mockups;
        }
    },

    // 合并参数
    SET_SETTING(state, payload) {
        // 标题为两个的的累加
        const title = state.clothes.map((i) => i.sub_title).join(" & ");
        const body = state.clothes.map((i) => i.description).join("<br>");
        const tags = state.clothes.map((i) => i.tags).join("|");
        const tags_list = tags.split("|").filter((i) => i);
        const repeat = tags_list.filter((c, i, a) => a.indexOf(c) == i);

        // 小于70个
        state.setting.title = title.substring(0, 70);
        state.setting.description = body;
        state.setting.tags = repeat.slice(0, 10).join("|");
    },

    SET_VARIANTS(state, payload) {
        // 利润 = 零售价 - 成本价
        // 利润率 = 利润 / 零售价
        // 零售价 = 成本价 / (1-利润率);

        const clothes = Array.isArray(payload) ? payload : [payload];

        const ss_currency = state.shop.ss_currency || "";
        const level_discount = state.setting.user_discount || 100;

        const tag_design_info = state.setting.tag_design_info || {};
        const pack_design_info = state.setting.pack_design_info || {};
        const tag_price = parseFloat(tag_design_info.total_price || 0);
        const pack_price = parseFloat(pack_design_info.total_price || 0);

        const rate = state.setting.rate;

        clothes.map((product) => {
            const variants = [];
            const inventory_list = product.inventory_list || [];
            const extra_price = parseFloat(product.extra_price) || 0;
            const blank_discount = product.is_blank ? 0.95 : 1;

            inventory_list.map((item) => {
                // skuObj[item.color_id + "-" + item.size_id] = item;
                // 商品价格
                const original = item.price + extra_price;

                // 成本价
                const cost_blank = Math.ceil(original * blank_discount);
                const cost_product = Math.round(
                    0.01 * cost_blank * level_discount
                );
                const cost_tag = Math.round(0.01 * tag_price * level_discount);
                const cost_pack = Math.round(
                    0.01 * pack_price * level_discount
                );

                const cost = Math.ceil(
                    rate * (cost_product + cost_tag + cost_pack)
                );

                const point = ss_currency != "USD" ? 0 : 40; // 默认利润率 40%
                const retail =
                    ss_currency != "USD" ? 0 : (100 * cost) / (100 - point);

                variants.push({
                    sku: item.sku,
                    original: original,
                    cost: cost, // 原价,向上取整
                    retail: mathMound(retail, 0), // 零售价
                    profit: mathMound(retail - cost, 0), // 利润
                    point: point, // 利润率
                    quality: item.quality, // 是否启用
                    color_id: item.color_id,
                    color_name: item.color_name,
                    color_num: item.color_num,
                    size_id: item.size_id,
                    size_name: item.size_name,
                });
            });

            product.variants = variants;
        });
    },
};

const actions = {
    // 设置内容

    // 绑定吊牌
    async getHangtag(store, tag_design_id) {
        if (tag_design_id) {
            // 从商店中获取
            const response = await this.$http("BrandHangtag.search", {});
            const hangtags = response.data || [];
            const active = hangtags.filter((i) => i.id == tag_design_id)[0];

            store.state.shop.tag_design_info = active || null;
        } else {
            store.state.shop.tag_design_info = null;
        }
    },

    // 绑定包装袋
    async getPackage(store, pack_design_id) {
        if (pack_design_id) {
            const response = await this.$http("BrandPackage.search", {});
            const hangpacks = response.data || [];
            const active = hangpacks.filter((i) => i.id == pack_design_id)[0];
            store.state.shop.pack_design_info = active || null;
        } else {
            store.state.shop.pack_design_info = null;
        }
    },

    // 获取商品信息
    async getShopInfo(store, shop_id) {
        const response = await this.$http("UserStore.getShopSetting", shop_id);
        if (response.code === 0) {
            const platform = {
                1: "shopify",
                2: "etsy",
                3: "wix",
                4: "woocommerce",
                5: "squarespace",
            };
            const data = response.data || {};

            store.state.shop.shop_id = shop_id;
            store.state.shop.shop_name = data.shop_name_text;
            store.state.shop.shop_type = data.shop_type;
            store.state.shop.platform = platform[data.shop_type];
            store.state.shop.ss_currency = data.ss_currency || "USD";
            store.state.shop.img_limit = data.image_limit || 10;

            // 创建同步时，绑定吊牌和包装袋
            if (store.state.status.action == "create") {
                const brands = [];
                brands.push(store.dispatch("getPackage", data.pack_design_id));
                brands.push(store.dispatch("getHangtag", data.tag_design_id));
                Promise.all(brands).then(() => store.commit("SET_BRAND"));
            }

            return response.data;
        } else {
            return Promise.reject(response.msg);
        }
    },

    // 获取模板数据
    async getDataByTemp(store, temp_id) {
        const response = await this.$http(
            "storeTemplate.getDetailInfo",
            temp_id
        );

        if (response.code == 0) {
            const data = response.data || {};

            const inventory = getInventoryFormat(data.inventory_list);
            // 子项
            const result = {
                spu: data.spu,
                product_id: data.product_id || 0, // 商品id
                design_id: temp_id || 0, // 设计id

                extra_price: data.extra_price, // 设计面加价
                is_blank: data.is_blank, // 空白模板

                tags: data.tags, // 标签
                title: data.template_name || "", // 标题
                description: data.description, // 商品描述
                sub_title: data.template_name || "", // 修改后的标题
                size_url: data.size_img_url || "", // 尺码
                tag_design_info: data.tag_design_info || null, // 吊牌
                pack_design_info: data.pack_design_info || null, // 包装袋

                piece_list: data.design_list || [], // 设计面列表
                model_tag: data.model_tag || "hanging", // 模特标签
                model_list: data.model_img || [], // 可选模特
                inventory_list: inventory.all_list || [], // 库存清单
                select_color: inventory.select_color || [], // 选中的颜色
                select_sizes: inventory.select_sizes || [], // 选中的尺码
                color_list: inventory.color_list || [], // 可选颜色
                sizes_list: inventory.sizes_list || [], // 可选尺码

                color_names: [], // 颜色名称
                variants: [],
            };

            // 数据格式化
            result.model_list.map((model) => {
                model.model_tag = model.model_tag || "hanging";
            });

            // 用户等级折扣
            store.state.setting = Object.assign(store.state.setting, {
                user_level: data.user_level,
                user_discount: data.user_discount,
            });

            store.state.clothes.push(result);
            store.state.mockups = [];

            // 设置变体名称
            store.commit("SET_COLOR_NAMES", result);
            // 设置setting
            store.commit("SET_SETTING");
            // 设置品牌
            store.commit("SET_BRAND");
            // 设置价格
            store.commit("SET_VARIANTS", store.state.clothes);

            return data;
        } else {
            return Promise.reject(response.msg);
        }
    },

    async getDataByStore(store, payload) {
        const goods_id = payload.goods_id;
        const response = await this.$http(
            "UserStore.getProductInfoSuit/" + goods_id,
            { shop_id: payload.shop_id }
        );

        if (response.code == 0) {
            const data = response.data || {};
            const product_map = new Map();
            const status = store.state.status;

            // 设置id
            store.state.status.id = goods_id;

            // 设置
            store.state.setting = Object.assign(store.state.setting, {
                title: data.title || "",
                description: data.body_html || "",
                tags: JSON.parse(data.tags || "[]").join("|"),
                display: !!data.display,
                taxonomy_id: data.taxonomy_id || "",
                is_new_shipping: false, // 二次编辑没有这个值
                shipping_profile_id: "", // 二次编辑没有这个值
                tag_design_info: data.tag_design_info,
                pack_design_info: data.pack_design_info,
                product_collection_id: data.product_collection_id || "",
                product_collection: data.product_collection || "",
                user_level: data.user_level || 1,
                user_discount: data.user_discount || 100,

                currency: data.currency,
                rate: data.rate || 1,
                price_notify: "change",
            });

            // 如果是已同步的二次编辑，则先提示
            if (store.rootState.currency != data.currency) {
                store.state.setting.price_notify = "notify";
            }

            store.state.clothes = (data.design_data || []).map((product) => {
                const inventory = getInventoryFormat(product.inventory_list);

                const color_names = (product.custom_color_infos || []).map(
                    (item) => {
                        return {
                            color_id: item.color_id,
                            color_num: item.color_num,
                            name: item.custom_name,
                        };
                    }
                );

                const result = {
                    spu: product.spu || "",
                    product_id: product.product_id || 0, // 商品id
                    shop_product_id: product.shop_product_id || 0, // 商品id
                    design_id: product.design_id || 0, // 设计id
                    extra_price: product.extra_price, // 设计面加价
                    is_blank: product.is_blank, // 空白模板
                    title: product.sub_title, // 标题
                    tags: "", // 标签
                    description: "", // 商品描述
                    sub_title: product.sub_title, // 修改后的标题
                    size_url: product.size_url || "", // 尺码

                    piece_list: JSON.parse(product.design || "[]"), // 设计面列表
                    model_tag: product.model_tag || "hanging", // 模特标签
                    model_list: product.model_list, // 可选模特
                    select_color: product.color, // 选中的颜色
                    select_sizes: product.size, // 选中的尺码
                    inventory_list: inventory.all_list || [], // 库存清单
                    color_list: inventory.color_list || [], // 可选颜色
                    sizes_list: inventory.sizes_list || [], // 可选尺码

                    color_names: color_names, // 颜色名称
                    variants: getVariantByStore(
                        store.state.setting,
                        product,
                        inventory.all_list
                    ),
                };

                // 数据格式化
                result.model_list.map((model) => {
                    model.model_tag = model.model_tag || "hanging";
                });

                product_map.set(result.design_id, product);
                return result;
            });

            // 选择的图片
            store.state.mockups_cache = data.image_infos || [];
            store.state.mockups = store.state.mockups_cache.map((item) => {
                // const surface_obj = { front: "Front Side", back: "Back Side" };
                // const surface_name = item.surface_name;
                const product = store.state.clothes.find(
                    (p) => p.design_id == item.design_id
                );
                const model_tag = item.model_tag || "hanging";
                const model =
                    item.type != "size" && product
                        ? product.model_list.find(
                              (m) =>
                                  m.surface_name == item.surface_name &&
                                  m.model_tag == model_tag
                          )
                        : null;

                // surface_name: surface_obj[surface_name] || surface_name, // 模特展示面front|back
                // 处理后台更换尺码表链接了的情况
                // if (item.type == "size") {
                //     const product = product_map.get(item.design_id);
                //     if (product && product.size_url != item.url) {
                //         item.url = product.size_url;
                //     }
                // }

                return {
                    type: item.type, // 类型： 主图main|普通normal|尺码size
                    design_id: item.design_id,
                    color_id: item.color_id,
                    model_id: model ? model.id : "",
                };
            });

            return data;
        } else {
            return Promise.reject(response.msg);
        }
    },

    // 初始化
    async init(store, payload = {}) {
        const promises = [];
        const state = store.state;

        state.clothes = [];
        state.mockups = [];
        state.mockups_cache = [];

        if (payload.goods_id) {
            // 二次编辑
            if (payload.action == "detail") {
                // 编辑详情
                state.status.step = 2;
                state.status.action = "detail";
            } else if (payload.action == "price") {
                // 编辑价格
                state.status.step = 3;
                state.status.action = "price";
            } else {
                // resave
                // 草稿状态下重新保存
                state.status.step = 2;
                state.status.action = "resave";
            }
            promises.push(store.dispatch("getShopInfo", payload.shop_id));
            promises.push(store.dispatch("getDataByStore", payload));
        } else {
            // 首次添加,初始化数据
            state.status.step = 1;
            state.status.action = "create";

            store.state.setting = Object.assign(store.state.setting, {
                title: "",
                description: "",
                tags: "",
                display: true,
                taxonomy_id: "",
                is_new_shipping: true,
                shipping_profile_id: "",
                tag_design_info: null,
                pack_design_info: null,
                product_collection_id: "",
                product_collection: "",
                currency: store.rootState.currency,
                rate: store.rootState.rate,
                price_notify: "change",
            });

            promises.push(store.dispatch("getShopInfo", payload.shop_id));
            promises.push(store.dispatch("getDataByTemp", payload.temp_id));
        }
        return await Promise.all(promises);
    },
};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions,
};

// var value = 0;
// var list = [1,2,3,10,11]
// var test = (key,val)=>{value = key >>> val; return list.map(i=>Boolean(i & value))}

// let r    = 0b100
// let w    = 0b010
// let x    = 0b001

// // 删除权限
// code = code & (~r);
// // 添加权限
// code = code | r;
// // 判断权限
// user & r === r

// // Math.pow(7, 3)

// test(1,1)
// test(2,1)
// test(3,1)
// test(1,1)

// test(1,0)
// test(2,0)
// test(3,0)
// test(1,0)
